package com.whately.core.base.jwt;

import com.whately.core.base.constant.LoginUser;
import com.whately.core.base.exception.ServiceError;
import com.whately.core.base.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录信息helper
 */
@Component
@Slf4j
public class JwtLoginUserComponent {
    /**
     *     获取登录信息
     *     未登陆则抛出异常
     */
    public LoginUser getLoginUser(HttpServletRequest request) {
        LoginUser loginInfo = FetchLoginUserHelper.getLoginUserWithDebugBuild(request);
        if (loginInfo == null) {
            throw new ServiceException(ServiceError.SESSION_TIME_OUT);
        }
        return loginInfo;
    }


    /**
     *     获取登录信息
     *     未返回null
     */
    public LoginUser getLoginUserWhenNotLoginReturnNull(HttpServletRequest request) {
        LoginUser loginInfo = FetchLoginUserHelper.getLoginUserWithDebugBuild(request);
        return loginInfo;
    }

}
